<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreApplication {
	var $base = NULL;
	var $objects = NULL;
	
	//constructor
	function CoreApplication() {
		
	}
	
	//methods
	function start() {
		
	}
	
	function excute() {
		
	}
	
	function inject() {
		
	}
	
	//call
	//	obj, class name or class object
	function call($obj, $method, $params = '', $type = '_array_') {
		if( empty($obj) ) {
			return -1;
		}
		if( !is_object($obj) ) {
			$obj = $this->getObj($obj);
			if( $obj == NULL ) {
				return -2;
			}
		}
		if( method_exists($obj, $method) ) {
			if( $type != '_array_' ) {
				$paramsb = array();
				$paramsb[] = $params;
				$paramsb[] = $type;
				$args = func_get_args();
				for($i=4, $n=sizeof($args); $i<$n; $i++) {
					$paramsb[] = $args[$i];
				}
				$params = $paramsb;
			} elseif( !is_array($params) ) {
				$paramsb = array();
				$paramsb[] = $params;
				$params = $paramsb;
			}
			//return call_user_func(array($obj, $method), $params);
			if( sizeof($params) == 0 ) {
				$data = $obj->$method();
			} elseif( sizeof($params) == 1 ) {
				$data = $obj->$method($params['0']);
			} elseif( sizeof($params) == 2 ) {
				$data = $obj->$method($params['0'], $params['1']);
			} elseif( sizeof($params) == 3 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2']);
			} elseif( sizeof($params) == 4 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3']);
			} elseif( sizeof($params) == 5 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4']);
			} elseif( sizeof($params) == 6 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5']);
			} else {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5'], $params['6']);
			}
			
			return $data;
		}
		
		return -3;
	}
	
	function callb($type, $method, $params = '') {
		$obj = $type;
		if( !is_object($type) ) {
			$class_file = $this->getClassAndFile($type);
			$obj = $class['0'];
		}
		
		return $this->call($obj, $method, $params = '');
	}
	
	function output() {
		
	}
	
	function close() {
		
	}
	
	//create
	function &getObj($classname, $classfile = '') {
		if( $classfile != '' ) {
			_lc($classfile);
		}
		if( class_exists($classname) ) {
			return new $classname();
		}
		
		$type = substr($classname, -4) == 'Base' ? substr($classname, 0, -4) : $classname;
		$class_file = $this->getClassAndFile($type);
		$classname = $class_file['0'];
		$classfile = $class_file['1'];
		
		//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $classfile);
		_lc($classfile);
		if( class_exists($classname) ) {
			return new $classname();
		}
		
		return NULL;
	}
	
	function getClassAndFile($type) {
		$type = strtolower($type);
		$class = '';
		$file = '';
		switch($type) {
			case 'content':
			case 'product':
			case 'staticpage':
			case 'link':
				$class = ucfirst($type) . 'Base';
				$file = $type . '.class.php';
				break;
			case 'contentcategory':
			case 'productcategory':
			case 'staticpagecategory':
			case 'linkcategory':
				$class = substr(ucfirst($type), 0, -8) . 'CategoryBase';
				$file = substr(ucfirst($type), 0, -8) . '.category.class.php';
				break;
			case 'c':
				$class = 'ContentBase';
				break;
			default:
				break;
		}
		
		return array($class, $file);
	}
}
//
?>